package com.thread;


public class Eat {
    static Integer sum  = 0;



    public static void main(String[] args) {
        Thread yuangong1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    synchronized (Eat.class)
                    {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if(sum<100)
                        {
                            sum++;
                            System.out.println("yuangong1做包子"+sum);

                        }
                    }

                }

            }
        });

        Thread yuangong2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    synchronized (Eat.class) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (sum < 100) {
                            sum++;
                            System.out.println("yuangong2做包子" + sum);
                        }
                    }
                }

            }
        });

        Thread guke1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    synchronized (Eat.class) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (sum > 0) {
                            System.out.println("guke1吃包子" + sum);
                            sum--;

                        } else {
                            System.out.println("等待做包子");
                        }
                    }
                }

            }
        });

        Thread guke2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    synchronized (Eat.class) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (sum > 0) {
                            System.out.println("guke2吃包子" + sum);
                            sum--;

                        } else {
                            System.out.println("等待做包子");
                        }
                    }
                }
            }
        });

        Thread guke3 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    synchronized (Eat.class) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (sum > 0) {
                            System.out.println("guke3吃包子" + sum);
                            sum--;

                        } else {
                            System.out.println("等待做包子");
                        }
                    }
                }
            }
        });

        Thread guke4 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    synchronized (Eat.class) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (sum > 0) {
                            System.out.println("guke4吃包子" + sum);
                            sum--;

                        } else {
                            System.out.println("等待做包子");
                        }
                    }
                }

            }
        });

        yuangong1.start();
        yuangong2.start();

        guke1.start();
        guke2.start();
        guke3.start();
        guke4.start();





    }
}
